package com.df.tv.launcher;

import android.app.Application;
import android.database.Cursor;
import android.net.Uri;

import com.df.tv.launcher.model.ClickLogFlushExecutor;
import com.df.tv.launcher.params.ComParams;
import com.df.tv.launcher.util.SystemInfo;
import com.df.tv.launcher.util.SystemInfo.NoSystemInfoException;
import com.df.tv.launcher.util.SystemInfo.SysInfo;
import com.telecom.tv189.comlib.models.ClickLogSender;
import com.telecom.tv189.comlib.net.XHttpClient;
import com.telecom.tv189.comlib.net.XHttpParams;
import com.telecom.tv189.comlib.switcher.MethodSwitcher;
import com.telecom.tv189.comlib.util.Log;
import com.telecom.tv189.comlib.util.SystemProperties;
import com.telecom.tv189.comlib.util.URLUtils;
import com.telecom.tv189.silentlogin.UserInfoKeeper;

public class LauncherApplication extends Application {

    private static final String TAG = "LauncherApplication";

    private static LauncherApplication instance = null;

    private static final String AUTHORITY = "com.telecom.tv189.tm.logswitch";
    private static final String LOG_SWITCH_INFO = "logswitch_info";
    private static final String LOG_INFO_SWITCH = "LogSwitch";

    public static LauncherApplication getInstance() {
        if (instance == null) {
            synchronized (LauncherApplication.class) {
                if (instance == null)
                    instance = new LauncherApplication();
            }
        }
        return instance;
    }

    public void onCreate() {
        super.onCreate();
        instance = this;
        UserInfoKeeper.getInstance().init(this, true);

        // log init
        boolean enableLog = getLogSwitch();
        Log.init(ComParams.LOG_TAG, true);

        //版本配置
        MethodSwitcher.addCase(ComParams.CASE_V4);
        MethodSwitcher.addCase(ComParams.CASE_V5);
        MethodSwitcher.setCurrentCase(getVersionCase());
        Log.i(TAG, "CurrentCase=" + MethodSwitcher.getCurrentCase());

        // 行为日志上报初始化
        ClickLogSender.getInstance(this).setCacheSize(ComParams.CLICK_LOG_CACHE);
        ClickLogSender.getInstance(this).setExecutor(new ClickLogFlushExecutor());

        // xhttp init
        XHttpParams.setComParams(ComParams.getInstance());
        String hostUrl = SystemProperties.get(ComParams.PROP_HOST_URL);
        if (!URLUtils.checkValidURL(hostUrl))
            hostUrl = ComParams.URL_HOST;
        Log.i(TAG, "HOST_URL=" + hostUrl);
        XHttpClient.setURL_HOST(hostUrl);
    }

    private String getVersionCase() {
        SysInfo sysInfo;
        try {
            sysInfo = SystemInfo.read();
        } catch (NoSystemInfoException e) {
            throw new RuntimeException(e);
        }
        try {
            String version = sysInfo.getVersion().trim();
            Log.i(TAG, "Version=" + version);
            if (version.startsWith(ComParams.VERSION_4X)) {
                return ComParams.CASE_V4;
            }
            else if (version.startsWith(ComParams.VERSION_5X)) {
                return ComParams.CASE_V5;
            }
            else
                throw new RuntimeException("unknow version");
        } catch (NullPointerException e) {
            //throw new RuntimeException("unknow version");
            return ComParams.CASE_V4;
        }
    }

    private boolean getLogSwitch() {
        String value = getString(R.string.system_debug_on);
        try {
            Uri uri = Uri.parse("content://" + AUTHORITY + "/" + LOG_SWITCH_INFO);
            Cursor cursor = getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            cursor.moveToFirst();
            value = cursor.getString(cursor.getColumnIndex(LOG_INFO_SWITCH));
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "true".equals(value);
    }
}
